package visao.veiculo;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import modelo.dominio.TipoVeiculo;
import modelo.dominio.Veiculo;
import modelo.persistencia.ControleAcesso;
import modelo.persistencia.PersistenciaTipoVeiculo;
import modelo.persistencia.PersistenciaVeiculo;

/*
 *Essa classe é responsável pela Interface que faz a Listagem dos Veiculos 
 * de uma determinada categoria
 */
public class InterfaceListagemVeiculoCategoria extends javax.swing.JFrame {
	
	private final PersistenciaVeiculo persistenciaVeiculo = new PersistenciaVeiculo();
	private final PersistenciaTipoVeiculo persistenciaTipoVeiculo = new PersistenciaTipoVeiculo();
	
	/*
	 * Construtor da classe Interface Listagem de veículo por Categoria
	 */
	public InterfaceListagemVeiculoCategoria() {
		initComponents();
		
		this.setTitle("Veiculos por Categoria");
		
		jLabel1.setText("Id da Categoria:");
		//jTextField1.setText("");
	}
	// fim Construtor
	
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
			  
            },
            new String [] {
                "Modelo","Placa","Marca","Cor","Observacoes","Tipo Veiculo"
            }
        ){
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class,
				java.lang.Integer.class
            };
            
            
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        DefaultTableCellRenderer esquerda = new DefaultTableCellRenderer();  
        DefaultTableCellRenderer centralizado = new DefaultTableCellRenderer();  
        esquerda.setHorizontalAlignment(SwingConstants.LEFT);  
        centralizado.setHorizontalAlignment(SwingConstants.CENTER);  
        jTable1.getColumnModel().getColumn(0).setCellRenderer(esquerda);  
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(1).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(9);
        jTable1.getColumnModel().getColumn(2).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(20);
        jTable1.getColumnModel().getColumn(3).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(20);
        jTable1.getColumnModel().getColumn(4).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(4).setPreferredWidth(110);
        jTable1.getColumnModel().getColumn(5).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(5).setPreferredWidth(30);
	   
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jButton1.setText("Logoff");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLogoff(evt);
            }
        });

        jButton2.setText("Voltar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoltar(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel1.setText("Categoria : ");

        jButton3.setText("Ok");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					jButton3ActionPerformed(evt);
				} catch (IOException ex) {
					Logger.getLogger(InterfaceListagemVeiculoCategoria.class.getName()).log(Level.SEVERE, null, ex);
				}
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 587, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addGap(31, 31, 31)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>

	/*
	 * Método do botão voltar que chma a janela do Menu Principal
	 * @param evt
	 */
	private void jButtonVoltar(java.awt.event.ActionEvent evt) {                               
		dispose();
		ControleAcesso.voltarMenuPrincipal();
	}                              
	// fim método

	/*
	 * Método do botão Log Off que chama no controle de acesso Log off
	 * @param evt
	 */
	private void jButtonLogoff(java.awt.event.ActionEvent evt) {                               
		dispose();
		ControleAcesso.logoff();
	}
	//fim método

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
			jTable1.setModel(new javax.swing.table.DefaultTableModel(
				new Object [][] {
			  
				},
				new String [] {
					"Modelo","Placa","Marca","Cor","Observacoes","Tipo Veiculo"
				}
			));
			String id = jTextField1.getText();
			if(!id.equals("")){
				int categoria = Integer.parseInt(id);
				TipoVeiculo novoTipoVeiculo = new TipoVeiculo(categoria);
				Veiculo novoVeiculo = new Veiculo(novoTipoVeiculo);
				
				if(novoTipoVeiculo != null){
					String modelo, placa, marca, cor, observacoes;
					TipoVeiculo tipoVeiculo;
					ArrayList<Veiculo> listVeiculo = persistenciaVeiculo.getListaPorCategoria(novoVeiculo);
					for(Veiculo veiculo: listVeiculo){
						modelo = veiculo.getModelo();
						placa = veiculo.getPlaca();
						marca = veiculo.getMarca();
						cor = veiculo.getCor();
						observacoes = veiculo.getObservacoes();
						tipoVeiculo = veiculo.getTipoVeiculo();
						((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{modelo,placa,marca,cor,observacoes,tipoVeiculo.getIdTipoVeiculo()});
					}
					jLabel1.setText("Id da Categoria:");
					jTextField1.setText("");
				}else{
					JOptionPane.showMessageDialog(null,"<html> <font color='black'>Naõ existe Tipo de Veículo com esse Id!","Erro",JOptionPane.ERROR_MESSAGE);
					jTextField1.setText("");
				}
			}else{
				JOptionPane.showMessageDialog(null,"<html> <font color='black'>Digite um Id do Tipo Veículo!","Erro",JOptionPane.ERROR_MESSAGE);
				jTextField1.setText("");
			}
	}

    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private int evento = 1;
}
// fim classe
